package android.tusnotas.model;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.tusnotas.beans.Folder;

public class FolderModel {
//INSERTS
	public long insertFolder(String name) {
		TusNotasModel.folderInsertStmt.bindString(1, name);
		return TusNotasModel.folderInsertStmt.executeInsert();
	}
	
	public long insertFolder(Folder folder,Integer courseId){
		TusNotasModel.folderInsertStmt.bindString(1, folder.getName());
		TusNotasModel.folderInsertStmt.bindDouble(2, courseId);
		return TusNotasModel.folderInsertStmt.executeInsert();
	}
	
//END INSERTS
	
//DELETES	
	public void deleteAllFolders() {
		TusNotasModel.db.delete(TusNotasModel.FOLDER_TABLE_NAME, null, null);
	}
	
	public void deleteCourseById(Integer id) {
		TusNotasModel.db.delete(TusNotasModel.FOLDER_TABLE_NAME, "id = "+id, null);
	}
//END DELETES

//SELECTS 	
	public List<Folder> selectAllFoldersByCourse(Integer courseId) {
		List<Folder> folders = new ArrayList<Folder>();
		Folder folder = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.FOLDER_TABLE_NAME, new String[] { "id", "name", "courseId"},
				"courseId ='"+ courseId +"'", null, null, null, "id ASC");
		
		if (cursor.moveToFirst()) {
			do {
				folder = new Folder();
				folder.setId(cursor.getInt(0));
				folder.setName(cursor.getString(1));
				folder.setCourseId(courseId);
				folders.add(folder);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return folders;
	}

}
